<?php
namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\model\Category;
use think\facade\Request;
use think\facade\View;

class Goods extends BaseController{
    //展示商品
    public function goodList()
    {
        //无线二级分类
        $cate=Category::category();
        View::assign('cate',$cate);
        list($where,$order)=\app\admin\logic\Goods::goodsWhere();
        $list=\app\admin\model\Goods::goodlist($where,$order);
        View::assign('list',$list);
        return view();
    }

    //添加商品
    public function goodExit()
    {
        //无线二级分类
        $cate=Category::category();
        View::assign('cate',$cate);
        //对应主键  和  修改的数据
        $id=Request::get('id');
        $goods=\app\admin\logic\Goods::xiuGoodWhere();
        $list=\app\admin\model\Goods::xiuGoodlist($id);
        View::assign($list);
        if(isset($goods)&&!empty($goods)){
            try{
                $goodsx=\app\admin\model\Goods::update($goods);
                $msg='修改成功';
            }catch(\Exception $e){
                $goodsx=false;
                $msg=$e->getMessage();
            }
            if ($goodsx){
                $this->success($msg,url('Goods/goodList'));
            }else{
                $this->error($msg);
            }
        }
        return view();
    }
    //商品添加
    public function goodAdd()
    {
        //无线二级分类
        $cate=Category::category();
        View::assign('cate',$cate);
        //接才能过来的所有参数
        $arr=\app\admin\logic\Goods::goodAddWhere();
        if (is_array($arr)){
            try{
                $list=\app\admin\model\Goods::goodAdd($arr);
                $msg='添加成功';
            }catch (\Exception $e){
                $list=false;
                $msg=$e->getMessage();
            }
            if ($list){
                $this->success($msg,url('Goods/goodList'));
            }else{
                $this->error($msg);
            }
        }


        return \view();
    }
    //商品删除
    public function goodDel()
    {
        //无线二级分类
        $cate=Category::category();
        View::assign('cate',$cate);
        list($where,$order)=\app\admin\logic\Goods::goodsWhere();
        $list=\app\admin\model\Goods::goodDel($where,$order);
        View::assign('list',$list);
        //软删除
        $delId=\app\admin\logic\Goods::goodsDel();
        if ($delId!=''){
            try{
                $list=\app\admin\model\Goods::goodListdel($delId);
                $msg='成功';
            }catch (\Exception $e){
                $list=false;
                $msg=$e->getMessage();
            }
            if ($list){
                $this->success($msg,url('Goods/goodList'));
            }else{
                $this->error($msg);
            }
        }



        //恢复字段
        $huiId=\app\admin\logic\Goods::huiGoodDelWhere();
        if ($huiId!=''){
            try{
                $list=\app\admin\model\Goods::huiGoodDel($huiId);
                $msg='成功';
            }catch (\Exception $e){
                $list=false;
                $msg=$e->getMessage();
            }
            if ($list){
                $this->success($msg,url('Goods/goodList'));
            }else{
                $this->error($msg);
            }
        }
        //真删字段
        $shanId=\app\admin\logic\Goods::shanGoodDelWhere();
        if ($shanId!=''){
            try{
                $list=\app\admin\model\Goods::zhenGoodDel($shanId);
                $msg='成功';
            }catch (\Exception $e){
                $list=false;
                $msg=$e->getMessage();
            }
            if ($list){
                $this->success($msg,url('Goods/goodDel'));
            }else{
                $this->error($msg);
            }
        }

        return view();
    }


}